using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using ShoppingCenter.Backend.Api.Model;
using ShoppingCenter.Backend.Api.Params;
using ShoppingCenter.Backend.Api.Repository;
using ShoppingCenter.Backend.Api.Utils;

namespace ShoppingCenter.Backend.Api.Controllers
{
    //[Authorize]
    [ApiController]
    [Route("[controller]")]

    //分类控制器
    public class CommodityCategoryController : ControllerBase
    {
        private IConfiguration _configuration;

        private IRepository<Commodity_category> _commodityCategoryRepository;

        public CommodityCategoryController(IConfiguration configuration, IRepository<Commodity_category> commodityCategoryRepository)
        {
            _configuration = configuration;
            _commodityCategoryRepository = commodityCategoryRepository;
        }

        //获取所有商品分类
        public dynamic Get()
        {
            var currentPage = int.Parse(Request.Query["currentPage"][0]);
            var pageSize = int.Parse(Request.Query["pageSize"][0]);

            var commodityCategorys = _commodityCategoryRepository.Table.ToList();

            // 分页
            var commodityCategoryPage = commodityCategorys.Skip((currentPage - 1) * pageSize).Take(pageSize); ;

            return new
            {
                Code = 1000,
                Data = new
                {
                    Data = commodityCategorys,
                    currentData = commodityCategoryPage,
                    Pager = new { currentPage, pageSize, totalSize = commodityCategorys.Count() }
                },
                Msg = "获取所有商品分类成功"
            };
        }

        //添加分类
        [HttpPost]
        public dynamic Post(CUR_CommodityCategory newCommodityCategory)
        {
            var commodityCategoryName = newCommodityCategory.Name.Trim();

            var ParentName = string.IsNullOrEmpty(newCommodityCategory.ParentName) ? null : newCommodityCategory.ParentName;

            if (string.IsNullOrEmpty(commodityCategoryName))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "当前分类名或不能为空"
                };
            }

            //如果没有父级分类
            if (ParentName == null)
            {
                var commodityCategory = new Commodity_category
                {
                    Name = commodityCategoryName,
                    GradeId = 1
                };

                _commodityCategoryRepository.Insert(commodityCategory);

                return new
                {
                    Code = 1000,
                    Data = commodityCategory,
                    Msg = "添加分类成功，没有父级"
                };


            }

            //获取父级分类
            var parentCategory = _commodityCategoryRepository.Table.Where(x => x.Name == ParentName).FirstOrDefault();

            if (parentCategory == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "当前父级分类无效"
                };
            }

            var commodityCategoryHaveParent = new Commodity_category
            {
                Name = commodityCategoryName,
                ParentCategoryId = parentCategory.Id,
                GradeId = parentCategory.GradeId + 1
            };

            _commodityCategoryRepository.Insert(commodityCategoryHaveParent);


            return new
            {
                Code = 1000,
                Data = commodityCategoryHaveParent,
                Msg = "添加分类成功"
            };

            //将结果实例化后输出

        }

        //根据id修改分类
        [HttpPut("{id}")]
        public dynamic Put(int id, CUR_CommodityCategory updateCommodityCategory)
        {

            //获取当前分类的名称
            var commodityCategoryName = updateCommodityCategory.Name.Trim();

            if (string.IsNullOrEmpty(commodityCategoryName))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "当前分类的名称不能为空"
                };
            }

            //通过Id获取要修改的分类
            var commodityCategory = _commodityCategoryRepository.GetById(id);

            if (commodityCategory == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "你要更新的分类不存在"
                };
            }

            commodityCategory.Name = commodityCategoryName;

            _commodityCategoryRepository.Update(commodityCategory);

            return new
            {
                Code = 1000,
                Data = commodityCategory,
                Msg = "更新分类成功"
            };

            // return JsonHelper.Serialize(res);

        }


        //批量删除子元素
        private void deleteChildren(List<Commodity_category> commodityCategory, int id)
        {

            commodityCategory.ForEach(x =>
            {
                 //判断当前分类的id有没有子分类
                if (x.ParentCategoryId == id)
                {
                    deleteChildren(commodityCategory, x.Id);

                    //删除当前分类
                    _commodityCategoryRepository.Delete(x.Id);
                }


            });
        }


        //根据id删除分类
        [HttpDelete("{id}")]
        public dynamic Dalete(int id)
        {

            var commodityCategory = _commodityCategoryRepository.GetById(id);

            if (commodityCategory == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = string.Format("无法删除id为{0}的分类", id)
                };
            }

            _commodityCategoryRepository.Delete(id);

            //获取所有分类
            var commodityCategorys = _commodityCategoryRepository.Table.ToList();


            deleteChildren(commodityCategorys, id);

            return new
            {
                Code = 1000,
                Data = "",
                Msg = "删除分类成功"
            };
        }


    }
}